#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include "Head/error.h"

#define MAXLINE 8192

static void sig_int(int); /* signal-catching function */

int main(int argc, char * aargv[]) {
	char	buf[MAXLINE];
	pid_t	pid;
	int 	status;

	if (signal(SIGINT, sig_int) == SIG_ERR)
		err_sys("signal error.");

	printf("%% ");
	while(fgets(buf, MAXLINE, stdin) != NULL) {
		buf[strlen(buf)-1] = 0;

		if( (pid = fork()) < 0)
			err_sys("fork error.");
		else if(pid == 0) { /* child */
			execlp(buf, buf, (char *)0);
			err_ret("couldn't execute: %s", buf);
			exit(127);
		}
		/* parent */
		if ((pid = waitpid(pid, &status, 0)) < 0)
			err_sys("Waitpid error.");
		printf("%% ");
	}

	exit(0);
}

void sig_int(int signo) {
	fprintf(stdout, "interrupt\n%% ");
}
